package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import org.apache.commons.lang3.StringUtils
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class api7MoorQueue implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(api7MoorQueue.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        String Syncpram = "";
        String Retupram = "";

        String getMethod = request.getMethod();
        if (getMethod.equals("GET")) {
            Syncpram = request.getQueryString();
            log.info("接收调用GET参数信息：" + Syncpram);
        } else if (getMethod.equals("POST")) {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                Syncpram += line;
            }

            log.info("接收调用POST报文信息：" + System.getProperty("line.separator") + Syncpram);
        }

        // 10072594|工单1|10|8|7|6&10072595|工单2|10|8|7|5&10072597|工单2|1|7|0|3

        try {

            Syncpram = URLDecoder.decode(Syncpram, "utf-8");

            String[] queueArr = Syncpram.split("#")[1].split("&");

            for (int i = 0; i < queueArr.size(); i++) {
                Map<String, String> headerMap = new HashMap<String, String>();

                String[] queueArrt = queueArr[i].split("\\|");

                headerMap.put("Queue", queueArrt[0]);
                headerMap.put("QueueName", queueArrt[1]);
                headerMap.put("Agent", queueArrt[2]);
                headerMap.put("Busy", queueArrt[3]);
                headerMap.put("Free", queueArrt[4]);
                headerMap.put("Wait", queueArrt[5]);

                // 先将数据推送到实时监控
                String EliteMonitorParam = "Queue=" + queueArrt[0] + "&QueueName=" + URLEncoder.encode(queueArrt[1], "utf-8") + "&Agent=" + queueArrt[2] + "&Free=" + queueArrt[4] + "&Busy=" + queueArrt[3] + "&ACW=0&AUX=0&Wait=" + queueArrt[5];

                // 1-获取其他服务器列表


                // 处理监控数据
                dealDNList(eliteBean, headerMap);

            }

        } catch (Exception e) {
            log.error("数据处理错误", e);
            Retupram = "-1"
        }

        return Retupram;
    }

    // 坐席状态推送处理
    public static String dealDNList(EliteBean eliteBean, Map<String, String> paramMap) {
        try {
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
            String Queue = objToString(paramMap.get("Queue"));
            String QueueName = objToString(paramMap.get("QueueName"));
            String Wait = objToString(paramMap.get("Wait"));

            log.debug("Queue:" + Queue + " QueueName:" + QueueName + " Wait:" + Wait);

            // 处理主数据
            if (!KHUtil.isNull(Queue)) {
                String dealSQL = "select * from dn_list where dn = ?";
                List<Map<String, Object>> selectCus = jt.queryForList(dealSQL, Queue);

                log.debug("selectCus：" + selectCus);
                log.debug("检查数据(" + Queue + ") 数量：" + selectCus.size());

                if (selectCus.size() == 0) {
                    String insertSQL = "Insert Into Dn_List (Dn, Type, Monitored, Dnname, Linkname, Wait_Num) Values (?, 3, 1, ?, 'link1', ?)";
                    int num = jt.update(insertSQL, Queue, QueueName, Wait);
                    log.debug("新增数据(" + Queue + ") 数量：" + num);
                } else {
                    String updateSQL = "Update Dn_List Set Dnname = ?, Wait_Num = ? Where Dn = ?";
                    int num = jt.update(updateSQL, QueueName, Wait, Queue);
                    log.debug("更新数据(" + Queue + ") 数量：" + num);
                }
            }

        } catch (Exception e) {
            log.error("数据处理错误", e);
            return "-1";
        }

        return "200";

    }

    public static String objToString(String[] obj) {

        if (obj == null) {
            return "";
        } else {
            return StringUtils.join(obj, "-");
        }
    }

}